# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
from datetime import datetime as dt


def return_securities_code():
    '''
    返回板块所有的证券的证券代码
    '''
    path = r'../附件/附件1.xlsx'
    sheet_num = range(37)

    securities = []
    for num in sheet_num:
        data = pd.read_excel(path, sheet_name=num, header=0, 
                 index_col=2, date_parser=True, nrows=1)

        code = data['证券名称'][0]
        securities.append(code)
    
    return securities

def generate_augment_total_capital():
    '''
    产生每一个股票的调整股本数
    上度娘自己查
    '''
    capital = {}
    capital['南玻A'] = 307069.21
    capital['深圳能源'] = 475738.99
    capital['东旭蓝天'] = 118949.912
    
    capital['方大集团'] = 75171.19
    capital['深赛格'] = 24713.125
    capital['宝鹰股份'] = 134129.69
    
    capital['东南网架'] = 1103440.22
    capital['延华智能'] = 71215.30
    capital['拓日新能'] = 123634.21
    capital['中利集团'] = 87178.71
    capital['亚厦股份'] = 133999.65
    
    capital['广田集团'] = 153727.97
    capital['瑞和股份'] = 37829.20
    capital['亚玛顿'] = 16000.00
    capital['永高股份'] = 123538.39
    capital['中装建设'] = 72144.58

    capital['南网能源'] = 75757.576
    capital['特锐德'] = 104071.07
    capital['嘉寓股份'] = 71676.00
    capital['东方日升'] = 90135.99
    capital['秀强股份'] = 61850.24
    
    capital['海达股份'] = 60123.42
    capital['旋极信息'] = 138207.248
    capital['中来股份'] = 87170.19
    capital['华自科技'] = 25617.15
    capital['启迪设计'] = 17413.90
    
    capital['汉嘉设计'] = 22573.83
    capital['精工钢构'] = 201287.43
    capital['苏美达'] = 130674.94
    capital['隆基股份'] = 386639.48
    capital['林洋能源'] = 174888.93

    capital['明阳智能'] = 156074.296
    capital['江河集团'] = 115405.00
    capital['中衡设计'] = 27680.77
    capital['森特股份'] = 53880.00
    capital['芯能科技'] = 35000.00
    capital['清源股份'] = 27380.00
    
    return capital

def data_read_fill(capital, end='2021-4-30'):
    '''
    数据读取与填充
    '''
    path = r'../附件/附件1.xlsx'
    sheet_num = range(1, 38)
    data_all = pd.DataFrame()
    for num in sheet_num:
        sheet_name = f'Sheet0 ({num})'
        try:
            data = pd.read_excel(path, sheet_name=sheet_name, header=0, 
                     index_col=2, date_parser=True)
        except:
            sheet_name = f'Sheet0({num})'
            data = pd.read_excel(path, sheet_name=sheet_name, header=0, 
                     index_col=2, date_parser=True)
            
        # 
        name = data['证券名称'][0]
        # 用上一次的值，填充缺失值    
        data_fillin = data.resample('D').mean()
        data_fillin.fillna(method='ffill', inplace=True)

        # 用 NAN 填补缺失数据
        time_begin = pd.to_datetime('2019-04-01')
        # 囊括的时间范围
        time_range = data.index
        # 数据的实际开始时间
        time_begin_data = time_range[0]
        
        
        if time_begin_data > time_begin:
            # 若股票代码的数据是晚于 2019 年 4 月 1 日开始的（大于表示晚）
            begin_row = pd.DataFrame(index=[time_begin], data=[])
            # 将 2019-4-1 日的数据插入
            data_fillin = pd.concat([begin_row, data_fillin], ignore_index = False)
         
        # 用 nan 填充不间断数据    
        data_fillin = data_fillin.resample('D').mean()
        data = data_fillin['2019-4-1':end]
        
        # 调整股本数 X 收盘价
        data = data['收盘价']*capital[name]
        data.rename(f'{name}_市值', inplace=True)
        
        data_all = pd.concat([data_all, data], axis=1)
        
        data_all.to_excel(f'../附件/中间数据/证券市值2019-4-1_{end}.xlsx')
    return data_all
        

if __name__ == '__main__':
    capital = generate_augment_total_capital()
    data_read_fill(capital, '2021-4-30')
    data_read_fill(capital, '2021-5-28')
    